package model;

import java.awt.image.BufferedImage;
import java.io.File;
import java.io.IOException;

import javax.imageio.ImageIO;
import javax.swing.ImageIcon;

public class SpriteType {
//each type of sprite share elements with this to flywheight all this stuff ;)
	private ImageIcon img;
	private ImageIcon collImg;
	private SimpleCollisionMap simpleCollisionMap;
	
	public SpriteType(ImageIcon img, SimpleCollisionMap simpleCollisionMap){
		this.img = img;
		this.simpleCollisionMap = simpleCollisionMap;
	}
	
	public SpriteType(String imageUrl,String collMapUrl) throws IOException{
		this.img = new ImageIcon(imageUrl);
		
		BufferedImage collMapImg = ImageIO.read( new File(collMapUrl) );
			this.simpleCollisionMap = new SimpleCollisionMap(collMapImg);
	}
	public SpriteType(String imageUrl) throws IOException{
		this(imageUrl,imageUrl);
	}
	
	protected ImageIcon getImg() {
		return img;
	}

	protected synchronized ImageIcon getCollisionMapImage(){
		
		if(collImg==null)createCollisionMapImage();
		
		return collImg;
		
	}
	
	private void createCollisionMapImage(){

		int[] color = {0,250,0,75};
		collImg=CollisionUtils.getCollisionMapImage(simpleCollisionMap, color);
	}
	
	protected void setImg(ImageIcon img) {
		this.img = img;
	}
	protected SimpleCollisionMap getCollisionMap() {
			return simpleCollisionMap;
	}
	protected void setCollisionMap(SimpleCollisionMap simpleCollisionMap) {
		this.simpleCollisionMap = simpleCollisionMap;
	} 
	
	public int getHeight(){
		return simpleCollisionMap.getHeight();
	}
	public int getWidth(){
		return simpleCollisionMap.getWidth();
	}
	
}
